package com.company.project.upm.controller.common;

import com.base.common.constants.HeaderConstants;
import com.base.common.model.dto.BaseDto;
import com.base.springboot.starter.web.controller.BaseController;
import com.company.project.upm.client.service.facade.common.HealthFacade;
import com.company.project.upm.service.common.HealthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 健康检查控制器
 * @Author zhongbo.yu
 * @Date 2021/12/28
 */
@Api(value = "健康检查控制器")
@RestController
@RequestMapping("health")
public class HealthController extends BaseController {

    @Autowired
    private HealthService healthService;
    @DubboReference
    private HealthFacade healthFacade;

    @GetMapping("checkHealth")
    @ApiOperation(value = "健康检查")
    public BaseDto checkHealth() {
        BaseDto checkAdmin = healthService.check();
        BaseDto checkApi = healthFacade.check();
        if (!HeaderConstants.STATUS_SUCCESS.equals(checkAdmin.getRespCode())) {
            return BaseDto.buildFailure("Admin服务异常");
        }
        if (!HeaderConstants.STATUS_SUCCESS.equals(checkApi.getRespCode())) {
            return BaseDto.buildFailure("Api服务异常");
        }
        return BaseDto.buildSuccess();
    }

}